﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ample.BLL.Store
{
    [Serializable]
    public class ShoppingCartItem
    {
        private int _id = 0;
        public int ID
        {
            get { return _id; }
            private set { _id = value; }
        }

        public int Line
        {
            get;
            set;
        }

        private string _title = "";
        public string Title
        {
            get { return _title; }
            private set { _title = value; }
        }

        private string _package = "";
        public string Package
        {
            get { return _package; }
            private set { _package = value; }
        }

        private string _description = "";
        public string Description
        {
            get { return _description; }
            private set { _description = value; }
        }


        private decimal _unitPrice;
        public decimal UnitPrice
        {
            get { return _unitPrice; }
            private set { _unitPrice = value; }
        }

        public ShoppingCartItem(int id, int line, string title, string package, string description, decimal unitPrice)        {
            this.ID = id;
            this.Title = title;
            this.Package = package;
            this.Description = description;
            this.UnitPrice = unitPrice;

            this.Line = line;
        }
    }

    [Serializable]
    public class ShoppingCartBO
    {
        private Dictionary<int, ShoppingCartItem> _items = new Dictionary<int, ShoppingCartItem>();
        public ICollection<ShoppingCartItem> Items
        {
            get { return _items.Values; }
        }

        /// <summary>
        /// Gets the sum total of the items' prices
        /// </summary>
        public decimal Total
        {
            get
            {
                decimal sum = 0.0m;
                foreach (ShoppingCartItem item in _items.Values)
                    sum += item.UnitPrice;
                return sum;
            }
        }

        /// <summary>
        /// Adds a new item to the shopping cart
        /// </summary>
        public void InsertItem(int id, string title, string package, string description, decimal unitPrice)
        {
            //if (_items.ContainsKey(id))
            //    _items[id].Quantity += 1;
            //else
            int line = 0;
            
            if(_items.Count() != 0)
                line = _items[_items.Count()].Line;

            _items.Add(line + 1, new ShoppingCartItem(id, line + 1, title, package, description, unitPrice));
        }

        /// <summary>
        /// Removes an item from the shopping cart
        /// </summary>
        //public void DeleteItem(int id)
        //{
        //    if (_items.ContainsKey(id))
        //    {
        //        ShoppingCartItem item = _items[id];
        //        item.Quantity -= 1;
        //        if (item.Quantity == 0)
        //            _items.Remove(id);
        //    }
        //}

        /// <summary>
        /// Removes all items of a specified product from the shopping cart
        /// </summary>
        public void DeleteProduct(int line)
        {
            if (_items.ContainsKey(line))
            {
                _items.Remove(line);

            }
        }

        /// <summary>
        /// Updates the quantity for an item
        /// </summary>
        //public void UpdateItemQuantity(int id, int line)
        //{
        //    Datakeyes dk = new Datakeyes { ID = id, Line = line };
        //    if (_items.ContainsKey(dk))
        //    {
        //        //ShoppingCartItem item = _items[id];
        //        //item.Quantity = quantity;
        //        //if (item.Quantity <= 0)
        //            _items.Remove(dk);
        //    }
        //}

        /// <summary>
        /// Clears the cart
        /// </summary>
        public void Clear()
        {
            _items.Clear();
        }
    }    
}
